package com.copractice.data.entity.feedback;

import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import org.apache.commons.lang.builder.ToStringBuilder;

import com.copractice.data.entity.PersistentObject;
import com.copractice.data.entity.question.Question;
import com.copractice.data.entity.user.User;

/**
 * Represents Users feedback entity.
 *
 * @author Dmitry Churbanov (dmitry.churbanov@gmail.com)
 * @version 1.0 03/19/2009
 */
@Entity
@Table(name = "FEEDBACK")
@SuppressWarnings("serial")
public class UsersFeedback extends PersistentObject {
    
    /**
     * feedback text
     */
    private String feedback;

    /**
     * how many viewers saw this feedback
     */
    private Long viewers;

    /**
     * user who created this feedback
     */
    private User createdUser;

    /**
     * question to which this feedback was written for
     */
    private Question question;

    /**
     * list of feedback answers which contains all score data about this feedback
     * and answers of the question the feedback was written for
     */
    private List<FeedbackAnswer> feedbackAnswer;

    /**
     * @see edu.vt.intelligenttutor.data.entity.PersistentObject#getId()
     */
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Override
    public Long getId() {
        return super.getId();
    }

    /**
     * Returns feedback content
     *
     * @return feedback content
     */
    @Column(name = "FEEDBACK")
    public String getFeedback() {
        return feedback;
    }

    /**
     * Sets feedback content
     *
     * @param feedback the <code>String</code> to be set as feedback content
     */
    public void setFeedback(String feedback) {
        this.feedback = feedback;
    }

    /**
     * Returns how many times this feedback was viewed
     *
     * @return the <code>Long</code> object holding how many times this feedback was viewed
     */
    @Column(name = "VIEWERS")
    public Long getViewers() {
        return viewers;
    }

    /**
     * Sets how many times this object was viewed
     *
     * @param viewers the <code>Long</code> object which contains the value how
     *                many times this object was viewed
     */
    public void setViewers(Long viewers) {
        this.viewers = viewers;
    }

    /**
     * Returns <code>User</code> who owns this feedback
     *
     * @return  the <code>User</code> who owns this feedback
     */
    @ManyToOne(cascade = {CascadeType.PERSIST, CascadeType.REMOVE})
    @JoinColumn(name="USER_ID")
    public User getCreatedUser() {
        return createdUser;
    }

    /**
     * Sets <code>User</code> who owns this feedback
     *
     * @param user the <code>User</code> who owns this feedback
     */
    public void setCreatedUser(User user) {
        this.createdUser = user;
    }

    /**
     * Returns <code>Question</code> to which this feedback belongs to.
     *
     * @return the <code>Question</code> to which this feedback belongs to.
     */
    @ManyToOne(cascade = CascadeType.ALL)
    @JoinColumn(name="QUESTION_ID")
    public Question getQuestion() {
        return question;
    }

    /**
     * Sets <code>Question</code> to which this feedback belongs to.
     *
     * @param question the <code>Question</code> to which this feedback belongs to.
     */
    public void setQuestion(Question question) {
        this.question = question;
    }
    
    /**
     * Returns <code>FeedbackAnswer</code> which contains all score data
     * of this feedback and questions score
     * 
     * @return the list of <code>FeedbackAnswer</code> which contains all score data
     */
    @OneToMany(fetch = FetchType.LAZY, cascade = CascadeType.ALL, mappedBy = "feedback")
    public List<FeedbackAnswer> getFeedbackAnswer() {
        return feedbackAnswer;
    }

    /**
     * Sets <code>FeedbackAnswer</code> which contains all score data
     * of this feedback and questions score
     * 
     * @param feedbackAnswer the list of <code>FeedbackAnswer</code> which contains 
     * all score data
     */
    public void setFeedbackAnswer(List<FeedbackAnswer> feedbackAnswer) {
        this.feedbackAnswer = feedbackAnswer;
    }
    
    /**
     * Returns a string representation of the entity.
     * 
     * @return a string representation of the entity.
     */
    @Override
    public String toString() {
        return new ToStringBuilder(this)
                .append("id", getId())
                .append("feedback", feedback)
                .append("viewers", viewers)
                .append("createdUser", createdUser)
                .append("question", question)
                .toString();
    }    

}
